package com.example.androidaudiovideodemo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * author: renbing
 *
 * date: 2021/12/9
 *
 * des:
 */
class MainTaskListAdapter<T>(private val context: Context,private val datas: List<T>,private val itemClick: (position: Int,data: T) -> Unit) : RecyclerView.Adapter<MainTaskListAdapter<T>.ViewHolder>(){

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        val taskName = itemView.findViewById<TextView>(R.id.item_task_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_main_task_list,parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.taskName.text = datas[position].toString()

        holder.itemView.setOnClickListener {
            itemClick.invoke(position,datas[position])
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }
}